package com.adeng.springbootdemo.hello.controller;

import com.adeng.springbootdemo.annotation.MvcControllerMethodLog;
import com.adeng.springbootdemo.hello.controller.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author
 * @create 2018-04-22 09:17
 */
@Controller
@Slf4j
public class HelloController {

    @Autowired
    TestService testService;

    //    @HystrixCommand(fallbackMethod = "okFallback", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100"),
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "50")
//
//    })
//    @MvcControllerMethodLog
    @RequestMapping("/hello/v1/{str}")
    @ResponseBody
//    @Sentinelmonitor(bizName = "home 接口", isAllReturn = true)
    public String home(@PathVariable String str, HttpServletRequest request) {
        System.out.println("home 接口被调用");
        String attr = (String) request.getAttribute("cookieUrsAccount");

        System.out.println(attr);

        //业务代码内部使用哨兵
        testService.testNeteaseSentinel(str);
//        testNeteaseSentinel(str);

//        HelloController proxy = (HelloController) AopContext.currentProxy();
//        proxy.testNeteaseSentinel(str);

        return "Hello World v1!";
    }

//    /**
//     * 这种方式为什么会 织入失败
//     *
//     * @param argStr
//     * @return
//     */
//    @Sentinelmonitor(bizName = "home 接口", isAllReturn = true)
//    public Boolean testNeteaseSentinel(String argStr) {
//        if (argStr == null) {
//            System.out.println("testNeteaseSentinel 返回 true");
//            return true;
//        } else {
//            System.out.println("testNeteaseSentinel 返回 false");
//            return false;
//        }
//    }


/*//    @Sentinelmonitor(description = "exception 接口")
    @RequestMapping("/exception/v1")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "okFallback",
            commandProperties = {
                    @HystrixProperty(name = "execution.timeout.enabled", value = "true")               //该方法不做超时校验
            })
    String exception() throws Exception {
        System.out.println("exception 接口被调用");

        throw new Exception("exception 接口被调用时出现异常");
    }*/

    public String okFallback(String str) {
        System.out.println("okFallback 发生熔断");
        return "okFallback 发生熔断";
    }

    @MvcControllerMethodLog
    @RequestMapping("/hello/exception/{str}")
    @ResponseBody
    public String exception(@PathVariable String str) throws Exception {
        System.out.println("home 接口被调用");

        throw new Exception("测试异常类");
    }

}
